#ifndef __ICE_AGENT_H
#define __ICE_AGENT_H
#include <string>
#include <vector>
#include <rtc_base/third_party/sigslot/sigslot.h>
#include "ice/ice_transport_channe.h"
#include "base/event_loop.h"
#include "ice/port_allocator.h"
namespace xrtc
{

    class IceAgent : public sigslot::has_slots<>
    {

    public:
        IceAgent(EventLoop *el, PortAllocator *allocator);
        ~IceAgent();
        IceTransportState ice_state() { return _ice_state; }
        bool create_channel(EventLoop *el, const std::string &tranpport_name, IceCandidateComponent component);
        IceTransportChannel *get_channel(const std::string &transport_name, IceCandidateComponent component);
        void gathering_candidate(); // 收集candidate
        void set_ice_params(const std::string &transport_name, IceCandidateComponent component, const IceParameters &ice_params);
        void set_remote_ice_params(const std::string &transport_name, IceCandidateComponent component, const IceParameters &ice_params);
        sigslot::signal4<IceAgent *, const std::string &, IceCandidateComponent, const std::vector<Candidate> &> signal_candidate_allocate_done; // signal4 需要四个参数
        sigslot::signal2<IceAgent *, IceTransportState> signal_ice_state;

    private:
        std::vector<IceTransportChannel *>::iterator _get_channel(const std::string &transport_name, IceCandidateComponent component);
        void on_candidate_allocate_done(IceTransportChannel *channel, const std::vector<Candidate> &);
        void _on_ice_receving_state(IceTransportChannel *);
        void _on_ice_writable_state(IceTransportChannel *);
        void _on_ice_state_changed(IceTransportChannel *);
        void _update_state();

    private:
        EventLoop *_el;
        std::vector<IceTransportChannel *> _channels;
        PortAllocator *_allocator;
        IceTransportState _ice_state = IceTransportState::k_new;
    };

} // namespace xrtc

#endif